//
//  BIDNoticeDetailViewController.m
//  cxgov
//
//  Created by mal on 16/3/6.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDOutingReportDetailViewController.h"
#import "BIDOutingDetailCell.h"
#import "BIDOutingReviewCell.h"
#import "BIDMenuView.h"
#import "BIDCustomTextView.h"

//批复
static NSString *replyOutingReportURL = @"outingAndroid!reply.action";
//删除
static NSString *deleteOutingReportURL = @"outingAndroid!dropOutingReply.action";
//查看报备详情
static NSString *outingReportDetailURL = @"outingAndroid!viewReceiveOuting.action";

@interface BIDOutingReportDetailViewController ()<UIAlertViewDelegate, UITableViewDataSource, UITableViewDelegate>
{
    BOOL _bShowMenu;
    BIDMenuView *_menuView;
    NSString *_noticeContent;
    NSArray *_dataSourceArr;
    //审批意见列表
    NSArray *_reviewArr;
    BOOL _bRegister;
    BIDOutingDetailCell *_tempDetailCell;
    BIDOutingReviewCell *_tempReviewCell;
}
@end

@implementation BIDOutingReportDetailViewController
@synthesize outingReportId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报备详情";
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteOutingReport)];
    //
    _tableView.allowsSelection = NO;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tempDetailCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDOutingDetailCell" owner:self options:nil] lastObject];
    _tempReviewCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDOutingReviewCell" owner:self options:nil] lastObject];
    //
    _optionTextView.layer.borderColor = RGBACOLOR(182.f, 218.f, 235.f, 1.f).CGColor;
    _optionTextView.layer.cornerRadius = 3.f;
    _optionTextView.layer.borderWidth = 0.5f;
    _optionTextView.clipsToBounds = YES;
    _optionTextView.placeholder = @"请填写您的意见";
    _optionTextView.inputAccessoryView = self.toolBar;
    //
    _agreeBtn.layer.borderColor = RGBACOLOR(43.f, 168.f, 26.f, 1.f).CGColor;
    _agreeBtn.layer.borderWidth = 0.5f;
    _agreeBtn.layer.cornerRadius = 3.f;
    
    _disagreeBtn.layer.borderColor = RGBACOLOR(241.f, 131.f, 73.f, 1.f).CGColor;
    _disagreeBtn.layer.borderWidth = 0.5f;
    _disagreeBtn.layer.cornerRadius = 3.f;
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
}
//加载报备详情
- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], outingReportDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"outingReply.id=%@&userId=%@", outingReportId, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            _reviewArr = [[NSArray alloc] initWithArray:responseDic[@"reviewContents"]];
            //
            NSDictionary *outingDic = responseDic[@"outing"];
            _dataSourceArr = @[@[@{@"title":@"申请人", @"content":outingDic[@"createUserName"]},
                                 @{@"title":@"开始时间", @"content":outingDic[@"startTime"]},
                                 @{@"title":@"截止时间", @"content":outingDic[@"endTime"]},
                                 @{@"title":@"目的地", @"content":outingDic[@"address"]},
                                 @{@"title":@"事由", @"content":outingDic[@"reason"]}],
                            @[@{@"title":@"姓名", @"content":outingDic[@"agentName"]},
                              @{@"title":@"职务", @"content":outingDic[@"agentPosition"]},
                              @{@"title":@"办公电话", @"content":outingDic[@"cphone"]},
                              @{@"title":@"移动电话", @"content":outingDic[@"mphone"]},
                              @{@"title":@"备注", @"content":outingDic[@"agentRemark"]}]];
            //
            [_tableView reloadData];
        }
    }];
}

//批复
- (void)replyWithOptionType:(NSInteger)type
{
    NSString *strReplyContent = _optionTextView.text;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyOutingReportURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"outingReply.id=%@&outingReply.content=%@&outingReply.isAgree=%ld", outingReportId, strReplyContent, type];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"批复成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:2];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"批复失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}
//删除列表中的一条报备
- (void)deleteOutingReport
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteOutingReportURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"selectIds=%@", outingReportId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"删除成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

- (IBAction)agreeBtnHandler:(id)sender
{
    [self replyWithOptionType:1];
}

- (IBAction)disagreeBtnHandler:(id)sender
{
    [self replyWithOptionType:2];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 2)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if(alertView.tag == 1)
    {
        //删除成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSourceArr.count + (_reviewArr.count>0?1:0);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 2)
        return _reviewArr.count;
    else
    {
        NSArray *arr = _dataSourceArr[section];
        return arr.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier1 = @"oneRow";
    static NSString *identifier2 = @"twoRow";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib1 = [UINib nibWithNibName:@"BIDOutingDetailCell" bundle:nil];
        [tableView registerNib:nib1 forCellReuseIdentifier:identifier1];
        //
        UINib *nib2 = [UINib nibWithNibName:@"BIDOutingReviewCell" bundle:nil];
        [tableView registerNib:nib2 forCellReuseIdentifier:identifier2];
    }
    if(section == 2)
    {
        NSDictionary *dic = _reviewArr[row];
        BIDOutingReviewCell *reviewCell = [tableView dequeueReusableCellWithIdentifier:identifier2];
        reviewCell.titleLabel.text = dic[@"userName"];
        reviewCell.contentLabel.text = [[NSString alloc] initWithFormat:@"%@ %@", dic[@"isAgree"], dic[@"reviewContent"]];
        cell = reviewCell;
    }
    else
    {
        NSDictionary *dic = _dataSourceArr[section][row];
        BIDOutingDetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        detailCell.titleLabel.text = [[NSString alloc] initWithFormat:@"%@:", dic[@"title"]];
        detailCell.contentLabel.text = dic[@"content"];
        cell = detailCell;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.f;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    NSDictionary *dic = nil;
    if(section==2)
    {
        dic = _reviewArr[row];
        _tempReviewCell.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-8-16;
        _tempReviewCell.contentLabel.text = dic[@"content"];
        CGSize size = [_tempReviewCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        height = size.height>66.f?size.height+1:66.f;
    }
    else
    {
        dic = _dataSourceArr[section][row];
        _tempDetailCell.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-8-94;
        _tempDetailCell.contentLabel.text = dic[@"content"];
        CGSize size = [_tempDetailCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        height = size.height>44.f?size.height+1:44.f;
    }
    return height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 44)];
    [headerView setBackgroundColor:RGBACOLOR(241, 241, 241, 1)];
    //
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(headerView.frame), 1)];
    [lineLabel setBackgroundColor:[UIColor lightGrayColor]];
    //[headerView addSubview:lineLabel];
    //
    CGFloat labelWidth = 200;
    CGFloat labelHeight = CGRectGetHeight(headerView.frame);
    CGFloat posX = (CGRectGetWidth(headerView.frame)-labelWidth)/2;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(posX, 0, labelWidth, labelHeight)];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:SHIMO_COLOR];
    [label setFont:[UIFont systemFontOfSize:15.f]];
    label.text = section==1?@"代其主持工作同志":@"审批内容";
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0) return 0;
    else return 44.f;
}

@end
